package com.example.emb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.emb.entity.Stuworktable;
import com.example.emb.entity.Worktable;
import com.example.emb.mapper.StuworktableMapper;
import com.example.emb.mapper.WorktableMapper;
import com.example.emb.service.WorkService;
import com.example.emb.service.exception.WritingErrorException;
import com.example.emb.until.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Api(tags = "教师发布作业的api所在")
@RequestMapping("/zjgsEmb/TeacherWork")
public class TeacherWorkController {
    @Autowired
    WorkService workService;
    @Autowired
    WorktableMapper worktableMapper;
    @Autowired
    StuworktableMapper stuworktableMapper;

//    //发布作业相关的内容
//    @ApiOperation("设置作业模板,基础模板")
//    @GetMapping("/post/setModel")
//    private JsonResult<String> setModel(String model){
//        //这个是设置模板，而不是在作业中选择
//        try{
//            workService.changeBaseMode(model);
//        }catch (WritingErrorException e){
//            return new JsonResult(50011,e.getMessage());
//        }
//        return new JsonResult(200);
//    }

    @ApiOperation("初始化作业 // 其中model是vditor的内容 ，其中要有的有：classid（班级的id）, model ,ispushed(是不是发布了),repeateRate 重复率,workname 作业名称,两个时间开始和结束时间")
    @PostMapping("/post/initWork")
    private JsonResult<Integer> initWork(Worktable worktable){
        int workId=worktableMapper.insert(worktable);
        return new JsonResult(200,workId);
    }

    @ApiOperation("更新作业 // 其中model是vditor的内容")
    @PostMapping("/post/upDateWork")
    private JsonResult<Integer> updateWork(Worktable worktable){
        int workId=worktableMapper.updateById(worktable);
        return new JsonResult(200,workId);
    }


//    //能不能使用session
//    @ApiOperation("改变作业模板")
//    @PutMapping("/setModel")
//    //这个改变的是当前作业的模板
//    private JsonResult<String> changeModel(String model, String WorkId, HttpSession session){
//        //验证session
//        String type=session.getAttribute("Type").toString();
//        if(!type.equals("Teacher"))
//            return new JsonResult(50012,"权限不足");
//        workService.updateWork(worktableMapper.selectById(WorkId).setModel(model));
//        return new JsonResult(200,null);
//    }

//    @ApiOperation("删除作业模板")
//    @DeleteMapping("/DeleteModel")
//    //其实这个就是把现在这个模板的内容置空就好了
//    private JsonResult<String> deleteModel(String model,String WorkId,HttpSession session){
//        //前端配合清空页面数据
//        String type=session.getAttribute("Type").toString();
//        if(!type.equals("Teacher"))
//            return new JsonResult(50012,"权限不足");
//        workService.updateWork(worktableMapper.selectById(WorkId).setModel(model));
//        return new JsonResult(200,"success");
//    }

    @ApiOperation("发布作业")
    @PostMapping("/postWork")
    //对此要新建一个字段操作
    private JsonResult PostWork(String workid){
        worktableMapper.updateById(worktableMapper.selectById(workid).setIsPushed(1));
        return new JsonResult(200);
    }

    @ApiOperation("通过TeacherWorkId 查询教师布置的作业原型")
    @GetMapping("/getTeacherWorkById")
    private JsonResult<Worktable> getWorkTable(int workid){
        QueryWrapper<Worktable> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",workid);
        Worktable worktable=worktableMapper.selectOne(queryWrapper);
        return new JsonResult<Worktable>(200,worktable);
    }

    // todo 4-15
    @ApiOperation("修改作业")
    @PutMapping("/changeWork")
    private JsonResult ChangeWork(Worktable worktable,HttpSession session){
        String type=session.getAttribute("Type").toString();
        if(!type.equals("Teacher"))
            return new JsonResult(50012,"权限不足");
        workService.updateWork(worktable);
        return new JsonResult(200);
    }

    @ApiOperation("删除作业  //在前端提示是否删除作业 可用")
    @DeleteMapping("/DeleteWork")
    private JsonResult DeleteWork(int workid) throws Exception {
        //逐级删除所有的内容
        workService.deleteWork(workid);
        return new JsonResult(200);
    }

    @ApiOperation("根据条件查询作业列表/可以使用  可上传的是 isFinished/isFinal/Workname/classid 不上传也没关系")
    @GetMapping("/GetWorkByInfo")
    private JsonResult<List<Worktable>> GetWorkByInfo(Worktable worktable){
        QueryWrapper<Worktable> queryWrapper=new QueryWrapper<>();
        if(worktable.getIsFinished() != null) {
            queryWrapper.eq("isFinished",worktable.getIsFinished());
        }

        if(worktable.getIsFinal() != null) {
            queryWrapper.eq("isFinal",worktable.getIsFinal());
        }

        if(worktable.getWorkname() != null) {
            queryWrapper.eq("Workname",worktable.getWorkname());
        }
        if(worktable.getClassid() != null) {
            queryWrapper.eq("classid",worktable.getClassid());
        }
        List<Worktable> list=worktableMapper.selectList(queryWrapper);
        return new JsonResult<List<Worktable>>(200,list);
    }

    @ApiOperation("通传入的workid 查询所有学生上交的作业")
    @GetMapping("/GetStudentWorksByWorkID")
    private JsonResult<List<Stuworktable>> GetStudentWorksByWorkID(int TeacherWorkId){
        QueryWrapper<Stuworktable>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("workid", TeacherWorkId);
        List<Stuworktable> list=stuworktableMapper.selectList(queryWrapper);
        return new JsonResult<List<Stuworktable>>(200,list);
    }

    @ApiOperation("教师批改作业，给作业评分")
    @PostMapping("/CorrectHomeWork")
    private JsonResult CorrectHomeWork(double mark,int StuWorkId){
        Stuworktable stuworktable=stuworktableMapper.selectById(StuWorkId);
        stuworktable.setMark(mark);
        stuworktableMapper.updateById(stuworktable);
        return new JsonResult(200);
    }
}
